package com.bst.etl.controller;

import com.bst.common.annotation.Log;
import com.bst.common.core.controller.BaseController;
import com.bst.common.core.domain.AjaxResult;
import com.bst.common.core.page.TableDataInfo;
import com.bst.common.enums.BusinessType;
import com.bst.common.utils.poi.ExcelUtil;
import com.bst.etl.domain.EtlDrawBaseDatax;
import com.bst.etl.service.IEtlDrawBaseDataxService;
import com.bst.etl.vo.DataxCreateParamVO;
import com.bst.etl.vo.EtlDrawBaseDataxEXVO;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * dataxController
 * 
 * @author ruoyi
 * @date 2024-02-01
 */
@Controller
@RequestMapping("/base/etl/datax")
public class EtlDrawBaseDataxController extends BaseController
{
    private String prefix = "etl/datax";

    @Autowired
    private IEtlDrawBaseDataxService etlDrawBaseDataxService;

    @GetMapping()
    public String datax()
    {
        return prefix + "/datax";
    }

    @GetMapping("/selectByTemplateId")
    @ResponseBody
    public TableDataInfo selectByTemplateId (Integer templateId,String naFilter) {
        List<EtlDrawBaseDatax> list = etlDrawBaseDataxService.selectEtlDrawBaseDataxByIdTemplate(templateId,naFilter);
        return getDataTable(list);
    }

    /**
     * 查询datax列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(EtlDrawBaseDatax etlDrawBaseDatax)
    {
        startPage();
        List<EtlDrawBaseDatax> list = etlDrawBaseDataxService.selectEtlDrawBaseDataxList(etlDrawBaseDatax);
        return getDataTable(list);
    }

    /**
     * 导出datax列表
     */
    @Log(title = "datax", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(EtlDrawBaseDatax etlDrawBaseDatax)
    {
        List<EtlDrawBaseDatax> list = etlDrawBaseDataxService.selectEtlDrawBaseDataxList(etlDrawBaseDatax);
        ExcelUtil<EtlDrawBaseDatax> util = new ExcelUtil<EtlDrawBaseDatax>(EtlDrawBaseDatax.class);
        return util.exportExcel(list, "datax数据");
    }
    /**
     * 新增datax
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }
    /**
     * 新增datax
     */
    @GetMapping("/batchAdd")
    public String batchAdd()
    {
        return prefix + "/batchAdd";
    }

    /**
     * 新增保存datax
     */
    @Log(title = "datax", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestBody EtlDrawBaseDatax etlDrawBaseDatax)
    {
        return toAjax(etlDrawBaseDataxService.insertEtlDrawBaseDatax(etlDrawBaseDatax));
    }

    /**
     * 修改datax
     */
    @GetMapping("/edit/{idDrawDatax}")
    public String edit(@PathVariable("idDrawDatax") Long idDrawDatax, ModelMap mmap) throws JsonProcessingException {
        EtlDrawBaseDatax etlDrawBaseDatax = etlDrawBaseDataxService.selectEtlDrawBaseDataxByIdDrawDatax(idDrawDatax);
        EtlDrawBaseDataxEXVO exvo = new EtlDrawBaseDataxEXVO();
        exvo.setDatax(etlDrawBaseDatax);
        String config = etlDrawBaseDatax.getConfig();
        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode rootNode = objectMapper.readTree(config);
        JsonNode readerNote = rootNode.get("job").findValues("reader").get(0);
        JsonNode writerNote = rootNode.get("job").findValues("writer").get(0);
        exvo.setSourTable(readerNote.findPath("table").get(0)==null?"":readerNote.findPath("table").get(0).asText());
        exvo.setBeforeSql(writerNote.findValuesAsText("preSql").size()==0?"":writerNote.findValuesAsText("preSql").get(0));
        JsonNode whereSqlNode = readerNote.get("parameter").get("where");
        exvo.setWhereSql(whereSqlNode==null?"":whereSqlNode.textValue());
        JsonNode querySqlNode = readerNote.findPath("querySql").get(0);
        exvo.setQuerySql(querySqlNode==null?"":querySqlNode.textValue());
        exvo.setColumns(rootNode.findValuesAsText("column"));
        exvo.setSplitPk(readerNote.findValue("splitPk").textValue());
        mmap.put("exvo", exvo);
        return prefix + "/edit";
    }

    /**
     * 修改保存datax
     */
    @Log(title = "datax", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@RequestBody EtlDrawBaseDatax etlDrawBaseDatax)
    {
        return toAjax(etlDrawBaseDataxService.updateEtlDrawBaseDatax(etlDrawBaseDatax));
    }

    /**
     * 删除datax
     */
    @Log(title = "datax", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(etlDrawBaseDataxService.deleteEtlDrawBaseDataxByIdDrawDataxs(ids));
    }

    @PostMapping("/buildJson")
    @ResponseBody
    public AjaxResult buildJobJson(@RequestBody DataxCreateParamVO paramVO) {
        if (paramVO.getIdDbReader() == null) {
            return AjaxResult.success("读数据源为空");
        }
        return success(etlDrawBaseDataxService.buildJobJson(paramVO));
    }
}
